<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Material extends BaseModel
{
    use HasFactory;
    //材料管理
    protected $table = 'material';
    // 允许批量操作的字段
    protected $fillable = [
        'name',
        'code',
        'type_id',
        'model_number',
        'specification_id',
        'color_id',
        'unit',
        'supplier_id',
        'w_price',
        'd_price',
        'initial_inventory',
        'initial_price',
        'initial_min',
        'initial_max',
        'is_del',
        'created_at',
        'updated_at',
    ];

    public function scopeSearch($query,$params)
    {
        if(!empty($params['name'])){
            $query->where('name','like','%'.$params['name'].'%');
        }
        $query->where('is_del',$params['is_del']??2);
        return $query;
    }

    //供应商
    public function supplier(){
        return $this->hasOne(Supplier::class,'id','supplier_id');
    }

    //材料分类
    public function type(){
        return $this->hasOne(MaterialType::class,'id','type_id');
    }

    //材料颜色
    public function color(){
        return $this->hasOne(Color::class,'id','color_id');
    }

    //材料规格
    public function specification(){
        return $this->hasOne(MaterialSpecification::class,'id','specification_id');
    }


}
